import java.util.Arrays;

/**
 * @author LKQ
 * @date 2021/12/2 9:02
 * @description 求最大子数组和 时间复杂度为O(n)的求解方法。
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
        System.out.println(solution.maxSubArray(nums));
    }

    public int maxSubArray(int[] nums) {
        int n = nums.length;
        for (int i = 1; i < n; i++) {
            nums[i] += Math.max(nums[i-1], 0);
        }
        return Arrays.stream(nums).max().getAsInt();
    }
}
